home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Audio / LPCView / Source / LPCreate.m < prev    next >
Encoding:
Text File  |  1992-03-07  |  9.0 KB  |  424 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "LPCreate.h"
  5. #import "Dispatcher.h"
  6. #import <appkit/Application.h>
  7. #import <appkit/Window.h>
  8. #import <stdio.h>
  9. #import <appkit/View.h>
  10. #import <appkit/Cell.h>
  11. #import <appkit/OpenPanel.h>
  12. #import <strings.h>
  13. #import <sound/sound.h>
  14. #import <appkit/ScrollView.h>
  15. #import <appkit/Text.h>
  16. #import <streams/streams.h>
  17. #import <libc.h>
  18. #import <sys/wait.h>
  19.  
  20. @implementation LPCreate
  21.  
  22. + new
  23. {
  24.     self = [super new];
  25.     [NXApp loadNibSection:"LPCreate.nib" owner:self];
  26.     [LPCpoles setIntValue:[NXApp poles]];
  27.     [Plowcps setFloatValue:[NXApp lowcps]];
  28.     [Phighcps setFloatValue:[NXApp highcps]];
  29.     [Pframe setIntValue:[NXApp ptchframe]];
  30.     [LPCframes setIntValue:[NXApp frames]];
  31.     [Pinter setIntValue:[NXApp frames]];
  32.     [self show:self];
  33.     return self;
  34. }
  35.  
  36. - getLPCFile:sender
  37. {
  38.     id    openP;
  39.      const char *types[2];
  40.     
  41.     types[0] = "lpc";
  42.     types[1] = 0;
  43.     openP = [OpenPanel new];
  44.     if ([openP runModalForTypes:types]) {
  45.         strcpy(lpcFilename, [openP filename]);
  46.         [LPCfield setStringValue:lpcFilename];
  47.     }
  48.     return self;
  49. }
  50.  
  51. - go:sender
  52. {
  53.     int    frames, poles, frameS, id1, id2, status, fd;
  54.     float    inskip, dur, lowcps, highcps;
  55.     char    cmd[256], temp[36], type[2];
  56.     FILE    *stream;
  57.     NXStream    *diags;
  58.     
  59.     frames = [LPCframes intValue];
  60.     if (!frames) 
  61.         frames = 250;
  62.     [Pinter setIntValue:frames];
  63.     poles = [LPCpoles intValue];
  64.     if (!poles)
  65.         poles = [NXApp poles];
  66.     inskip = [LPCinskip floatValue];
  67.     if (!strcmp([LPCdur stringValue], "all")) {
  68.         dur = -1;
  69.     }
  70.     else {
  71.         dur = [LPCdur floatValue];
  72.     }
  73.     strcpy(lpcFilename, [LPCfield stringValue]);
  74.     if (!strlen(lpcFilename)) {
  75.         [self badFile:"LPC"];
  76.         return self;
  77.     }
  78.     strcpy(soundFilename, [Soundfield stringValue]);
  79.     if (!strlen(soundFilename)) {
  80.         [self badFile:"Sound"];
  81.         return self;
  82.     }
  83.     strcpy(pchFilename, [Pitchfield stringValue]);
  84.     if (!strlen(pchFilename)) {
  85.         [self badFile:"Pitch analysis"];
  86.         return self;
  87.     }
  88.     sprintf(cmd, "%s -o %s -p %d -f %d ", [NXApp lpc], lpcFilename, poles, frames);
  89.     if (inskip > 0) {
  90.         sprintf(temp, "-i %f ", inskip);
  91.         strcat(cmd, temp);
  92.     }
  93.     if (dur > 0) {
  94.         sprintf(temp, "-d %f ", dur);
  95.         strcat(cmd, temp);
  96.     }        
  97.     strcat(cmd, soundFilename);
  98.     printf("calling lpc....\n");
  99.     printf("cmd: %s\n", cmd);
  100.     system(cmd);
  101.     sprintf(cmd, "%s %s %d", [NXApp stabilize], lpcFilename, poles);    
  102.     printf("calling stablilize.....\n");    
  103.     printf("cmd: %s\n", cmd);
  104.     system(cmd);
  105.     frameS = [Pframe intValue];
  106.     if (!frameS)
  107.         frameS = 350;
  108.     lowcps = [Plowcps floatValue];
  109.     if (!lowcps)
  110.         lowcps = 100.0;
  111.     highcps = [Phighcps floatValue];
  112.     if (!highcps) 
  113.         highcps = 1000.0;
  114.     inskip = [Pinskip floatValue];
  115.     if (!strcmp([Pdur stringValue], "all")) {
  116.         dur = 99;
  117.     }
  118.     else {
  119.         dur = [LPCdur floatValue];
  120.     }
  121.     strcpy(pchFilename, [Pitchfield stringValue]);
  122.     type[0] = 'w';
  123.     type[1] = '\0';
  124.     printf("calling ptrack.....\n");
  125.  
  126.     if (!(id1 = fork())) {
  127.         fd = open("/tmp/output", O_CREAT|O_RDWR, 0644);
  128.         dup2(fd, fileno(stderr));
  129.         sprintf(cmd, "%s -q ", [NXApp ptrack]);
  130.         stream = popen(cmd, type);
  131.         fprintf(stream, "%s\nn\n%s\n%d\n%d\n%f\n%f\n%f\n%f\n", soundFilename, pchFilename, frameS, frames, lowcps, highcps, inskip, dur);
  132.         fclose(stream);
  133.         while ((wait((union wait *)&status) != -1)) 
  134.             ;
  135.         close(fd);
  136.         exit(1);
  137.     }
  138.     while ((wait((union wait *)&status) != -1))
  139.         ;
  140.     diags = NXMapFile("/tmp/output", NX_READONLY);
  141.     [[diagScroll docView] readText:diags];
  142.     NXCloseMemory(diags, NX_FREEBUFFER);
  143.     if (!(id2 = fork())) {
  144.         printf("Calling merge : %s\n", [NXApp merge]);
  145.         fd = open("/tmp/output", O_CREAT|O_WRONLY|O_TRUNC, 0644);
  146.         dup2(fd, fileno(stdout));
  147.         stream = popen([NXApp merge], type);
  148.         fprintf(stream, "%s\n%s\n%d\n1\n1\n999\n", lpcFilename, pchFilename, poles);
  149.         fclose(stream);
  150.         while ((wait((union wait *)&status) != -1))
  151.             ;
  152.         close(fd);
  153.         exit(1);
  154.     }
  155.     while ((wait((union wait *)&status) != -1))
  156.         ;
  157.     diags = NXMapFile("/tmp/output", NX_READONLY);
  158.     [[diagScroll docView] selectAll:self];
  159.     [[diagScroll docView] cut:self];
  160.     [[diagScroll docView] readText:diags];
  161.     [[diagScroll docView] setSel:0:0];
  162.     [[diagScroll docView] paste:self];
  163.     NXCloseMemory(diags, NX_FREEBUFFER);
  164.     printf("done!\n");
  165.     return self;
  166. }
  167.  
  168. - doLPC:sender
  169. {
  170.     int    frames, poles;
  171.     float    inskip, dur;
  172.     char    cmd[256], temp[36];
  173.  
  174.     frames = [LPCframes intValue];
  175.     if (!frames) 
  176.         frames = 250;
  177.     [Pinter setIntValue:frames];
  178.     poles = [LPCpoles intValue];
  179.     if (!poles)
  180.         poles = 24;
  181.     inskip = [LPCinskip floatValue];
  182.     if (!strcmp([LPCdur stringValue], "all")) {
  183.         dur = -1;
  184.     }
  185.     else {
  186.         dur = [LPCdur floatValue];
  187.     }
  188.     strcpy(lpcFilename, [LPCfield stringValue]);
  189.     if (!strlen(lpcFilename)) {
  190.         [self badFile:"LPC"];
  191.         return self;
  192.     }
  193.     strcpy(soundFilename, [Soundfield stringValue]);
  194.     if (!strlen(soundFilename)) {
  195.         [self badFile:"Sound"];
  196.         return self;
  197.     }
  198.     strcpy(pchFilename, [Pitchfield stringValue]);
  199.     if (!strlen(pchFilename)) {
  200.         [self badFile:"Pitch Analysis"];
  201.         return self;
  202.     }
  203.     sprintf(cmd, "%s -o %s -p %d -f %d ", [NXApp lpc], lpcFilename, poles, frames);
  204.     if (inskip > 0) {
  205.         sprintf(temp, "-i %f ", inskip);
  206.         strcat(cmd, temp);
  207.     }
  208.     if (dur > 0) {
  209.         sprintf(temp, "-d %f ", dur);
  210.         strcat(cmd, temp);
  211.     }        
  212.     strcat(cmd, soundFilename);
  213.     printf("calling lpc....\n");
  214.     printf("cmd: %s\n", cmd);
  215.     system(cmd);
  216.     sprintf(cmd, "%s %s %d", [NXApp stabilize], lpcFilename, poles);    
  217.     printf("calling stablilize.....\n");    
  218.     printf("cmd: %s\n", cmd);
  219.     system(cmd);
  220.     return self;
  221. }
  222.  
  223. - doPtrack:sender
  224. {
  225.     int    frames, frameS, id1, status;
  226.     float    inskip, dur, lowcps, highcps;
  227.     char    type[2], cmd[256];
  228.     FILE    *stream;
  229.     
  230.     frames = [LPCframes intValue];
  231.     frameS = [Pframe intValue];
  232.     if (!frameS)
  233.         frameS = 350;
  234.     lowcps = [Plowcps floatValue];
  235.     if (!lowcps)
  236.         lowcps = 100.0;
  237.     highcps = [Phighcps floatValue];
  238.     if (!highcps) 
  239.         highcps = 1000.0;
  240.     inskip = [Pinskip floatValue];
  241.     if (!strcmp([Pdur stringValue], "all")) {
  242.         dur = 99;
  243.     }
  244.     else {
  245.         dur = [LPCdur floatValue];
  246.     }
  247.     strcpy(soundFilename, [Soundfield stringValue]);
  248.     if (!strlen(soundFilename)) {
  249.         [self badFile:"Sound"];
  250.         return self;
  251.     }
  252.     strcpy(pchFilename, [Pitchfield stringValue]);
  253.     if (!strlen(pchFilename)) {
  254.         [self badFile:pchFilename];
  255.         return self;
  256.     }
  257.     type[0] = 'w';
  258.     type[1] = '\0';
  259.     printf("calling ptrack.....\n");
  260.  
  261.     if (!(id1 = fork())) {
  262.         sprintf(cmd, "%s -q ", [NXApp ptrack]);
  263.         stream = popen("/Net/dobro/musr/bin/ptrack -q ", type);
  264.         fprintf(stream, "%s\nn\n%s\n%d\n%d\n%f\n%f\n%f\n%f\n", soundFilename, pchFilename, frameS, frames, lowcps, highcps, inskip, dur);
  265.         fclose(stream);
  266.         while ((wait((union wait *)&status) != -1)) 
  267.             ;
  268.         exit(1);
  269.     }
  270.     while ((wait((union wait *)&status) != -1))
  271.         ;
  272.  
  273.     return self;
  274. }
  275.  
  276. - doMerge:sender
  277. {
  278.     int    id1, status, poles;
  279.     char    type[2];
  280.     FILE    *stream;
  281.     NXStream    *output;
  282.     
  283.     strcpy(lpcFilename, [LPCfield stringValue]);
  284.     if (!strlen(lpcFilename)) {
  285.         [self badFile:"LPC"];
  286.         return self;
  287.     }
  288.     strcpy(pchFilename, [Pitchfield stringValue]);
  289.     if (!strlen(pchFilename)) {
  290.         [self badFile:"Pitch Analysis"];
  291.         return self;
  292.     }
  293.     poles = [LPCpoles intValue];
  294.     if (!poles) {
  295.         poles = [NXApp poles];
  296.     }
  297.     type[0] = 'w';
  298.     type[1] = '\0';
  299.     if (!(id1 = fork())) {
  300.         output = NXOpenFile(id1, NX_READWRITE);
  301.         stream = popen([NXApp merge], type);
  302.         fprintf(stream, "%s\n%s\n%d\n1\n1\n999\n", lpcFilename, pchFilename, poles);
  303.         fclose(stream);
  304.         NXClose(output);
  305.         while ((wait((union wait *)&status) != -1))
  306.             ;
  307.         exit(1);
  308.     }
  309.  
  310.     while ((wait((union wait *)&status) != -1))
  311.         ;
  312.     
  313.     return self;
  314. }
  315.  
  316. - viewLPC:sender
  317. {
  318.     strcpy(lpcFilename, [LPCfield stringValue]);
  319.     if (!strlen(lpcFilename))
  320.         return self;
  321.     [NXApp viewFile:lpcFilename];
  322.     return self;
  323. }
  324.  
  325. - viewPCH:sender
  326. {
  327.     strcpy(pchFilename, [Pitchfield stringValue]);
  328.     if (!strlen(pchFilename))
  329.         return self;
  330.     [NXApp viewPCHfile:pchFilename];
  331.     return self;
  332. }
  333.  
  334. - playSound:sender
  335. {
  336.     int    err;
  337.     SNDSoundStruct *s;
  338.     
  339.     if (!strlen(soundFilename))
  340.         return self;
  341.     err = SNDReadSoundfile(soundFilename, &s);
  342.     if (!err) {
  343.         err = SNDStartPlaying(s, 1, 5, 0, 0,(SNDNotificationFun)SNDFree);
  344.         if (!err)
  345.             SNDWait(1);
  346.     }
  347.     return self;
  348. }
  349.  
  350. - doHZ:sender
  351. {
  352.     float    val;
  353.     char    cmd[256];
  354.     
  355.     val = [HZfield floatValue];
  356.     sprintf(cmd, "%s %f\n", [NXApp hz], val);
  357.     system(cmd);
  358.     return self;
  359. }
  360.  
  361. - getPitchFile:sender
  362. {
  363.     id    openP;
  364.      const char *types[2];
  365.     
  366.     types[0] = "pch";
  367.     types[1] = 0;
  368.     openP = [OpenPanel new];
  369.     if ([openP runModalForTypes:types]) {
  370.         strcpy(pchFilename, [openP filename]);
  371.         [Pitchfield setStringValue:pchFilename];
  372.     }
  373.     return self;
  374. }
  375.  
  376. - getSoundFile:sender
  377. {
  378.     id    openP;
  379.     const char *types[2];
  380.     char    *t, *s;
  381.     
  382.     types[0] = "snd";
  383.     types[1] = 0;
  384.     
  385.     openP = [OpenPanel new];
  386.     if ([openP runModalForTypes:types]) {
  387.         strcpy(soundFilename, [openP filename]);
  388.         [Soundfield setStringValue:soundFilename];
  389.         t = rindex(soundFilename, '/');
  390.         s = rindex(t, '.');
  391.         if (!strlen(lpcFilename)) {
  392.             strncpy(lpcFilename, soundFilename, (int)(s - soundFilename));
  393.             strcat(lpcFilename, ".lpc");
  394.             [LPCfield setStringValue:lpcFilename];
  395.         }
  396.  
  397.         if (!strlen(pchFilename)) {
  398.             strncpy(pchFilename, soundFilename, (int)(s - soundFilename));
  399.             strcat(pchFilename, ".pch");
  400.             [Pitchfield setStringValue:pchFilename];
  401.         }
  402.     }
  403.     return self;
  404. }
  405.  
  406. - show:sender
  407. {
  408.     [myWin makeKeyAndOrderFront:self];
  409.     return self;
  410. }
  411.  
  412. - badFile:(char *)type
  413. {
  414.     NXRunAlertPanel("Alert", "Invalid %s filename\n", "Ok", NULL, NULL, type);
  415.     return self;
  416. }
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423. @end
  424.